﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;

namespace ControlExplorer
{
    public static class DataTools
    {
        /// <summary>
        /// 返回模板的Stream对象
        /// </summary>
        /// <param name="templateid">模板编号</param>
        /// <returns></returns>
        public static Stream GetTemplateStream(string templateid)
        {
            DataRow dr = GetTemplateInfo(templateid);
            byte[] templatebytes = dr["模板"] as byte[];
            System.IO.MemoryStream ms = new System.IO.MemoryStream(templatebytes);

            return ms;
        }

        /// <summary>
        /// 返回模板全部信息
        /// </summary>
        /// <param name="templateid">模板编号</param>
        /// <returns></returns>
        public static DataRow GetTemplate(string templateid)
        {
            return GetTemplateInfo(templateid);
        }

        /// <summary>
        /// 模板数据字段
        /// </summary>
        /// <param name="templateid">模板编号</param>
        public static List<string> GetTemplateDataFields(string templateid)
        {
            List<string> columns = new List<string>();

            DataTable dt = new DataTable();
            string tablename = GetTemplateDataSource(templateid);
            string commandstring = string.Format("SELECT TOP 1 * FROM [{0}]", tablename);
            string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;

            using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionstring))
            {
                System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(commandstring, connection);
                System.Data.OleDb.OleDbCommandBuilder builder = new System.Data.OleDb.OleDbCommandBuilder(adapter);
                adapter.Fill(dt);
            }

            for (int i = 1; i < dt.Columns.Count; i++)
            {
                columns.Add(dt.Columns[i].ColumnName);
            }

            return columns;
        }

        /// <summary>
        /// 模板数据源名称
        /// </summary>
        /// <param name="templateid"></param>
        /// <returns></returns>
        public static string GetTemplateDataSource(string templateid)
        {
            DataRow dr = GetTemplateInfo(templateid);
            return dr["数据源"].ToString();
        }

        // 返回模板全部信息
        private static DataRow GetTemplateInfo(string templateid)
        {
            DataTable dt = new DataTable();
            string commandstring = string.Format("SELECT * FROM 模板列表 WHERE 模板编号='{0}'", templateid);
            string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;

            using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionstring))
            {
                System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(commandstring, connection);
                System.Data.OleDb.OleDbCommandBuilder builder = new System.Data.OleDb.OleDbCommandBuilder(adapter);
                adapter.Fill(dt);
            }
            
            return dt.Rows[0];
        }
    }
}